package controllers
/*********************************************************/
/****************本文档中的程序用于机器学习相关***************/
/*********************version:1.0*************************/
/****Copyright (c) 2014年 许剑华. All rights reserved******/
/*********************************************************/
import (
	"fmt"
	"time"
	."smart_home/lib"
	."smart_home/models"
	"github.com/astaxie/beego"
)

//用于接收关于开关控制的channel
var switch_auto_contr = make(chan P_SET_SWITCH, 1000)
//用于接收关于空调控制的channel
var remote_auto_contr = make(chan SetRemoteType, 1000)


//将客户端的控制过程记录下来，用于机器学习
func AutoControl(){
	for{
		select {
		case status := <- switch_auto_contr:     //接收信道数据
			fmt.Println("switch_auto_contr",status)
			hour,min,_ := time.Now().Clock()
			size,err := GetParticleSize()
			if err == nil{
				TI := hour*60+(min/size)*size			//计算当前的时间
//				fmt.Println("time",hour,min,sec,TI)

				//data := status.(SetRemoteType)
				mTimerEvent := TimerEvent{Mac:status.Mac,EventTime:TI,DeviceType:status.DeviceType,TimeScale:INVALID,IRFRQ:INVALID,OpType:"",Model:SET_DEVICE_STATUS,RespCode:INVALID,Mask:status.Mask}
				SetTimerEvent(mTimerEvent,TI)			//触发定时事件
			}
		case status := <- remote_auto_contr:		 //接收信道数据
			fmt.Println("remote_auto_contr",status)
			hour,min,_ := time.Now().Clock()
			size,err := GetParticleSize()
			if err == nil{
				TI := hour*60+(min/size)*size
//				fmt.Println("time",hour,min,sec,TI)

				//data := status.(SetRemoteType)
				mTimerEvent := TimerEvent{Mac:status.Mac,EventTime:TI,DeviceType:status.DeviceType,TimeScale:status.TimeScale,IRFRQ:status.IRFRQ,OpType:status.OpType,Model:SET_REMOTE_STATUS,RespCode:INVALID,Mask:INVALID}
				SetTimerEvent(mTimerEvent,TI)//触发定时事件
			}
		}
	}
}

//将关于开关的控制，加入信道中
func AddAutoContrSw(data P_SET_SWITCH){
	switch_auto_contr <- data
}

//将关于红外的控制，加入信道中
func AddAutoContrRe(data SetRemoteType){
	remote_auto_contr <- data
}

//机器通过学习记录，实现自主控制设备
func AutoTimer(){
	timer2 := time.NewTicker(10 * time.Second)
	for {
		select {
		case <-timer2.C:
			//fmt.Println("timer")

			size,err := GetParticleSize()			//获取控制粒度
			if err == nil{
				hour,min,_ := time.Now().Clock()
				time := hour*60+(min/size)*size
//				fmt.Println("size",size)
//				fmt.Println("timer",time)
				data,num,_ := GetCountBigToThreshold(time)
//				fmt.Println("GetCountBigToParticleSize(10)",data)
				var i int64
				for i = 0;i < num; i++ {
					if data[i].Model == SET_DEVICE_STATUS{
						//data[i].High0,data[i].High1
						Enable,_:=GetMashineLearn(data[i].Mac)
						if Enable == true{

							SendCmd(data[i].Mac,REQ_RESP_Type{RespCode:INVALID,Mac:data[i].Mac,Model:data[i].Model,DeviceType:data[i].DeviceType,IP:INVALIDS,Port:INVALID,ProductName:INVALIDS,TimeScale:data[i].TimeScale,IRFRQ:data[i].IRFRQ,OpType:data[i].OpType,Mask:data[i].Mask})
							beego.Info("AutoTimer SendCmd switch\n")
						}
					}else if data[i].Model == SET_REMOTE_STATUS {
						Enable,_:=GetMashineLearn(data[i].Mac)
						if Enable == true {
							//code_time := CodeTimeType{Load0:data[i].Load0, Load1:data[i].Load1, High:[2]int32{data[i].High0, data[i].High1}, Low:[2]int32{data[i].Low0, data[i].Low1}}
							//SendCmd(data[i].Mac, REQ_RESP_Type{data[i].Mac, data[i].DeviceType, data[i].TimeScale,data[i].IRFRQ, data[i].OpType, SET_REMOTE_STATUS, INVALID, data[i].Mask})
							SendCmd(data[i].Mac,REQ_RESP_Type{RespCode:INVALID,Mac:data[i].Mac,Model:data[i].Model,DeviceType:data[i].DeviceType,IP:INVALIDS,Port:INVALID,ProductName:INVALIDS,TimeScale:data[i].TimeScale,IRFRQ:data[i].IRFRQ,OpType:data[i].OpType,Mask:data[i].Mask})
							beego.Info("AutoTimer SendCmd REMOTE\n")

						}
					}
				}
			}
		}
	}
}
